package com.clstu.socket;

import com.sun.media.jfxmediaimpl.HostUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketTCP01Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);//监听9999端口并且等待连接
        System.out.println("服务器在9999端口监听，等待连接-----");
        Socket socket = serverSocket.accept();//如果有人来连接9999端口就返回一个Socket对象
        System.out.println("服务器端 socket ========= "+socket.getClass());

        InputStream inputStream = socket.getInputStream();//得到输入流对象，可以接受数据
        int readLen = 0;
        byte[] buf = new byte[1024];
        while ((readLen=inputStream.read(buf)) != -1){
            System.out.println(new String(buf,0,readLen));//读出数据 并显示到显示器上
        }

        inputStream.close();
        socket.close();
        serverSocket.close();
        System.out.println("服务端已经退出=========");
    }
}
